﻿package mortal.game.control
{
    import Framework.Util.*;
    import Message.Game.*;
    import Message.Public.*;
    import com.gengine.utils.*;
    import com.mui.controls.*;
    import extend.language.*;
    import flash.events.*;
    import mortal.common.*;
    import mortal.common.error.*;
    import mortal.common.global.*;
    import mortal.component.gconst.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.proxy.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.utils.*;
    import mortal.game.view.market.*;
    import mortal.game.view.market.alert.*;
    import mortal.game.view.market.myQiugou.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class MarketController extends Controller
    {
        private var _marketCache:MarketCache;
        private var _marketProxy:MarketProxy;
        private var _marketModule:MarketModule;
        private var _marketItemRecord:SMarketItem;

        public function MarketController()
        {
            this.init();
            return;
        }// end function

        private function init() : void
        {
            this._marketCache = cache.market;
            this._marketProxy = GameProxy.market;
            return;
        }// end function

        override protected function initView() : IView
        {
            if (this._marketModule == null)
            {
                this._marketModule = new MarketModule();
                this._marketModule.addEventListener(WindowEvent.SHOW, this.onMarketShow);
                this._marketModule.addEventListener(WindowEvent.CLOSE, this.onMarketClose);
            }
            return this._marketModule;
        }// end function

        override protected function initServer() : void
        {
            Dispatcher.addEventListener(EventName.MarketSearch, this.searchHandler);
            Dispatcher.addEventListener(EventName.MarketBuyMarketItem, this.buyHandler);
            Dispatcher.addEventListener(EventName.MarketSellItem2SeekBuy, this.saleSeekBuyHandler);
            Dispatcher.addEventListener(EventName.MarketClickQickBuy, this.clickQickBuyHandler);
            return;
        }// end function

        protected function onMarketShow(event:Event) : void
        {
            Dispatcher.addEventListener(EventName.MarketSale, this.saleHandler);
            Dispatcher.addEventListener(EventName.MarketQiugou, this.qiugouHandler);
            Dispatcher.addEventListener(EventName.MarketCancelSeekBuy, this.cancelSeekBuyHandler);
            Dispatcher.addEventListener(EventName.MarketCancelSell, this.cancelSellHandler);
            Dispatcher.addEventListener(EventName.MarketGetMyRecords, this.getMyRecordHandler);
            Dispatcher.addEventListener(EventName.MarketBroadcast, this.broadcastHandler);
            Dispatcher.addEventListener(EventName.MarketError, this.onErrorHandler);
            return;
        }// end function

        protected function onMarketClose(event:Event) : void
        {
            Dispatcher.removeEventListener(EventName.MarketSale, this.saleHandler);
            Dispatcher.removeEventListener(EventName.MarketQiugou, this.qiugouHandler);
            Dispatcher.removeEventListener(EventName.MarketCancelSeekBuy, this.cancelSeekBuyHandler);
            Dispatcher.removeEventListener(EventName.MarketCancelSell, this.cancelSellHandler);
            Dispatcher.removeEventListener(EventName.MarketGetMyRecords, this.getMyRecordHandler);
            Dispatcher.removeEventListener(EventName.MarketBroadcast, this.broadcastHandler);
            Dispatcher.removeEventListener(EventName.MarketError, this.onErrorHandler);
            return;
        }// end function

        private function onErrorHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as Exception;
            MsgManager.showRollTipsMsg(ErrorCode.getErrorStringByCode(_loc_2.code));
            if (_loc_2.code == 36453)
            {
            }
            if (_loc_2.code == 36454)
            {
                if (!MarketMyQiugouPanel.instance.isHide)
                {
                    MarketMyQiugouPanel.instance.getDataFromServer();
                }
                if (!MarketMySalePanel.instance.isHide)
                {
                    MarketMySalePanel.instance.getDataFromServer();
                }
                (view as MarketModule).refresh();
            }
            return;
        }// end function

        private function qiugouHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data;
            this._marketProxy.seekBuy(_loc_2.code, _loc_2.amount, _loc_2.price, _loc_2.unit, _loc_2.time, _loc_2.broadcast);
            return;
        }// end function

        private function cancelSellHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data;
            this._marketProxy.cancelSell(_loc_2.recordId);
            return;
        }// end function

        private function cancelSeekBuyHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data;
            this._marketProxy.cancelSeekBuy(_loc_2.recordId);
            return;
        }// end function

        private function saleHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data;
            this._marketProxy.sellItem(_loc_2.code, _loc_2.itemUid, _loc_2.amount, _loc_2.price, _loc_2.unit, _loc_2.time, _loc_2.broadcast);
            return;
        }// end function

        private function searchHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data;
            this._marketProxy.search(_loc_2.recordType, _loc_2.marketId, _loc_2.codes, _loc_2.targetPage, _loc_2.levelLower, _loc_2.levelUpper, _loc_2.color, _loc_2.career, _loc_2.order, _loc_2.playerName);
            return;
        }// end function

        private function clickQickBuyHandler(event:DataEvent) : void
        {
            NetDispatcher.addCmdListener(ServerCommand.MarketItemRecordUpdate, this.getMarketItemRecordBack);
            this._marketProxy.getMarketRecordById(event.data as Number);
            return;
        }// end function

        private function getMarketItemRecordBack(param1:Object) : void
        {
            NetDispatcher.removeCmdListener(ServerCommand.MarketItemRecordUpdate, this.getMarketItemRecordBack);
            if (this._marketCache.marketItemRecord == null || this._marketCache.marketItemRecord.recordId == -1)
            {
                MsgManager.showRollTipsMsg(Language.getString(20415));
                return;
            }
            if (this._marketCache.marketItemRecord.recordType == EMarketRecordType._EMarketRecordSell)
            {
                this.buyHandler(new DataEvent("", this._marketCache.marketItemRecord));
            }
            else if (this._marketCache.marketItemRecord.recordType == EMarketRecordType._EMarketRecordSeekBuy)
            {
                this.saleSeekBuyHandler(new DataEvent("", this._marketCache.marketItemRecord));
            }
            return;
        }// end function

        private function buyHandler(event:DataEvent) : void
        {
            var _loc_4:String = null;
            this._marketItemRecord = event.data as SMarketItem;
            var _loc_2:* = new ItemData(this._marketItemRecord.code);
            if (!Cache.instance.role.enoughMoney(this._marketItemRecord.sellUnit, this._marketItemRecord.sellPrice))
            {
                MsgManager.showRollTipsMsg(Language.getString(20416));
                return;
            }
            var _loc_3:* = Language.getString(20417) + HTMLUtil.addColor(this._marketItemRecord.amount.toString(), GlobalStyle.colorHuang);
            if (this._marketItemRecord.code == EPriceUnit._EPriceUnitCoin || this._marketItemRecord.code == EPriceUnit._EPriceUnitGold)
            {
                var _loc_7:* = _loc_3 + GameDefConfig.instance.getEPrictNameAddColoer(this._marketItemRecord.code, GlobalStyle.colorHuang);
                _loc_3 = _loc_3 + GameDefConfig.instance.getEPrictNameAddColoer(this._marketItemRecord.code, GlobalStyle.colorHuang);
                _loc_4 = _loc_7;
            }
            else
            {
                _loc_3 = _loc_3 + (Language.getString(20419) + ItemsUtil.getItemName(_loc_2));
            }
            _loc_4 = _loc_3 + Language.getString(20419) + HTMLUtil.addColor(MktModConfig.getTotalPrice(this._marketItemRecord), GlobalStyle.colorHuang) + GameDefConfig.instance.getEPrictNameAddColoer(this._marketItemRecord.sellUnit) + Language.getString(20420);
            var _loc_5:String = "";
            if (this._marketItemRecord.sellUnit == EPriceUnit._EPriceUnitCoin)
            {
                _loc_5 = TodayNoTipsConst.MarketBuyByCoin;
            }
            else if (this._marketItemRecord.sellUnit == EPriceUnit._EPriceUnitGold)
            {
                _loc_5 = TodayNoTipsConst.MarketBuyByGold;
            }
            var _loc_6:Object = {};
            _loc_6.unit = this._marketItemRecord.sellUnit;
            _loc_6.str = Language.getString(20417);
            Alert.extendObj = _loc_6;
            TodayNotTipUtil.toDayNotTip(_loc_5, this.onBuySelect, _loc_4, null, Alert.OK | Alert.CANCEL, null, null, null, 4, MktTodayNoTipAlert);
            return;
        }// end function

        private function onBuySelect() : void
        {
            this._marketProxy.buyMarketItem(this._marketItemRecord.recordId);
            return;
        }// end function

        private function saleSeekBuyHandler(event:DataEvent) : void
        {
            var _loc_4:ItemData = null;
            var _loc_2:* = event.data as SMarketItem;
            var _loc_3:* = Cache.instance.role.money;
            if (_loc_2.code == EPriceUnit._EPriceUnitCoin)
            {
                if (_loc_3.coin < 10000)
                {
                    MsgManager.showRollTipsMsg(Language.getString(20421));
                    return;
                }
            }
            else if (_loc_2.code == EPriceUnit._EPriceUnitGold)
            {
                if (_loc_3.gold < 1)
                {
                    MsgManager.showRollTipsMsg(Language.getString(20421));
                    return;
                }
            }
            else
            {
                _loc_4 = new ItemData(_loc_2.code);
                if (_loc_4 == null || Cache.instance.pack.backPackCache.getItemCountByItemData(_loc_4, false) <= 0)
                {
                    MsgManager.showRollTipsMsg(Language.getString(20422));
                    return;
                }
            }
            MktSaleQiugouAlert.instance().setData(_loc_2);
            MktSaleQiugouAlert.instance().show();
            MktSaleQiugouAlert.instance().updateUI();
            MktSaleQiugouAlert.instance().okFunction = this.onSaleSeekBuySelect;
            return;
        }// end function

        private function onSaleSeekBuySelect(param1:Object) : void
        {
            var _loc_2:* = param1.recordId;
            var _loc_3:* = param1.uid;
            var _loc_4:* = param1.amount;
            this._marketProxy.sellItem2SeekBuy(_loc_2, _loc_3, _loc_4);
            return;
        }// end function

        private function getMyRecordHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data;
            this._marketProxy.getMyRecords(_loc_2.recordType);
            return;
        }// end function

        private function broadcastHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data;
            this._marketProxy.broadcastMarketRecord(_loc_2.recordId);
            return;
        }// end function

    }
}
